﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Forms;
using Scan.PDAService;
using Scan.DAO;

namespace Scan
{
    class ArchivesInventorys
    {
        private static void DeleteInventorys()
        {
           // SQLiteHelper.ExecuteNonQuery("Delete from Inventorys", null);
            SQLiteHelper.ExecuteNonQuery("Delete from Inventorys", null);
        }

        //下载数据
        public static bool DownloadInventorys()
        {
            //执行 WebService 方法
            ResponseBaseOfInventory response = GlobalCache.Instance.WebServiceIfe.GetInventorys(false, "", "", "");

            if (response.ErrorInfo.ErrorCode == 1)
            {
                if (response.Lists != null || response.Lists.Length != 0)
                {
                    DeleteInventorys();

                    string sql = "insert into Inventorys (cInvCode, cInvName, cInvStd, cInvType, cBarCode, cUnitCode, cUnitName, cAssitUnitCode, cAssitUnitName, iAssitUnitRate, iRetailPrice, iLowSalePrice, iRectInPrice, iRectOutPrice) "
                               + "values (@cInvCode, @cInvName, @cInvStd, @cInvType, @cBarCode, @cUnitCode, @cUnitName, @cAssitUnitCode, @cAssitUnitName, @iAssitUnitRate, @iRetailPrice, @iLowSalePrice, @iRectInPrice, @iRectOutPrice)";
                    SqlCeParameter[] parameters = new SqlCeParameter[14];
                    parameters[0] = new SqlCeParameter();
                    parameters[0].ParameterName = "@cInvCode";
                    parameters[0].SqlDbType = SqlDbType.NVarChar;
                    parameters[1] = new SqlCeParameter();
                    parameters[1].ParameterName = "@cInvName";
                    parameters[1].SqlDbType = SqlDbType.NVarChar;
                    parameters[2] = new SqlCeParameter();
                    parameters[2].ParameterName = "@cInvStd";
                    parameters[2].SqlDbType = SqlDbType.NVarChar;
                    parameters[3] = new SqlCeParameter();
                    parameters[3].ParameterName = "@cInvType";
                    parameters[3].SqlDbType = SqlDbType.NVarChar;
                    parameters[4] = new SqlCeParameter();
                    parameters[4].ParameterName = "@cBarCode";
                    parameters[4].SqlDbType = SqlDbType.NVarChar;
                    parameters[5] = new SqlCeParameter();
                    parameters[5].ParameterName = "@cUnitCode";
                    parameters[5].SqlDbType = SqlDbType.NVarChar;
                    parameters[6] = new SqlCeParameter();
                    parameters[6].ParameterName = "@cUnitName";
                    parameters[6].SqlDbType = SqlDbType.NVarChar;
                    parameters[7] = new SqlCeParameter();
                    parameters[7].ParameterName = "@cAssitUnitCode";
                    parameters[7].SqlDbType = SqlDbType.NVarChar;
                    parameters[8] = new SqlCeParameter();
                    parameters[8].ParameterName = "@cAssitUnitName";
                    parameters[8].SqlDbType = SqlDbType.NVarChar;
                    parameters[9] = new SqlCeParameter();
                    parameters[9].ParameterName = "@iAssitUnitRate";
                    parameters[9].SqlDbType = SqlDbType.Int;
                    parameters[10] = new SqlCeParameter();
                    parameters[10].ParameterName = "@iRetailPrice";
                    parameters[10].SqlDbType = SqlDbType.NVarChar;
                    parameters[11] = new SqlCeParameter();
                    parameters[11].ParameterName = "@iLowSalePrice";
                    parameters[11].SqlDbType = SqlDbType.NVarChar;
                    parameters[12] = new SqlCeParameter();
                    parameters[12].ParameterName = "@iRectInPrice";
                    parameters[12].SqlDbType = SqlDbType.NVarChar;
                    parameters[13] = new SqlCeParameter();
                    parameters[13].ParameterName = "@iRectOutPrice";
                    parameters[13].SqlDbType = SqlDbType.NVarChar;

                    // 循环插入客商档案
                    foreach (PDAService.Inventory Inventory in response.Lists)
                    {
                        //parameters[0].Value = Inventory.InvCode;
                        //parameters[1].Value = Inventory.InvName;
                        //parameters[2].Value = Inventory.InvStd;
                        //parameters[3].Value = Inventory.InvType;
                        //parameters[4].Value = Inventory.BarCode;
                        //parameters[5].Value = Inventory.UnitCode;
                        //parameters[6].Value = Inventory.UnitName;
                        //parameters[7].Value = Inventory.AssitUnitCode;
                        //parameters[8].Value = Inventory.AssitUnitName;
                        //parameters[9].Value = Inventory.AssitUnitRate;
                        //parameters[10].Value = Inventory.RetailPrice;
                        //parameters[11].Value = Inventory.LowSalePrice;
                        //parameters[12].Value = Inventory.RectInPrice;
                        //parameters[13].Value = Inventory.RectOutPrice;

                        SQLiteHelper.ExecuteNonQuery(sql, parameters);
                    }
                }
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取商品档案
        /// </summary>
        /// <param name="Fuzzyed">模糊查询</param>
        /// <param name="BarCode">条形码</param>
        /// <param name="InvCode">商品编码</param>
        /// <param name="InvName">商品名称</param>
        public static Inventory[] GetInventorys(Boolean Fuzzyed, string BarCode, string InvCode, string InvName)
        {
            if (Configuration.Instance.OnLineType == OnLineType.Online)
            {   // 在线方式

                //执行 WebService 方法
                ResponseBaseOfInventory response = GlobalCache.Instance.WebServiceIfe.GetInventorys(Fuzzyed, BarCode, InvCode, InvName);
                if (response.ErrorInfo.ErrorCode == 1)
                {
                    if (response.Lists != null || response.Lists.Length != 0)
                    {
                        return response.Lists;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    MessageBox.Show(response.ErrorInfo.ErrorMsg, "获取商品档案出错！");
                    return null;
                }
            }
            else
            {   // 离线方式
                string sSQL = "select cInvCode, cInvName, cInvStd, cInvType, cBarCode, cUnitCode, cUnitName, cAssitUnitCode, "
                            + "cAssitUnitName, iAssitUnitRate, iRetailPrice, iLowSalePrice, iRectInPrice, iRectOutPrice from Inventorys";
                return null;
            }
        }

 
    }
}
